package com.ankur.fareplease;

import java.text.NumberFormat;

import com.ankur.fareplease.filters.DecimalDigitsInputFilter;
import com.ankur.fareplease.model.FareDAO;
import com.ankur.fareplease.service.AutoFareService;
import com.ankur.fareplease.service.impl.AutoFareServiceImpl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class AutoActivity extends Activity {
	Spinner citySelect;
	CheckBox night_charge_check;
	EditText kms_input;
	TextView final_fare;
	NumberFormat numFormat;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		numFormat=NumberFormat.getInstance();
		numFormat.setMaximumFractionDigits(2);
		numFormat.setMinimumFractionDigits(2);
		setContentView(R.layout.autoinput);
		citySelect = (Spinner) findViewById(R.id.auto_city_select);
		night_charge_check=(CheckBox) findViewById(R.id.auto_night_charge_check);
		kms_input=(EditText) findViewById(R.id.auto_kms_input);
		kms_input.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(1)});
		final_fare=(TextView) findViewById(R.id.auto_final_fare);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.auto_city_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		citySelect.setAdapter(adapter);

		Button getFareButton = (Button) findViewById(R.id.auto_fare_button);
		getFareButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FareDAO fareDAO=new FareDAO();
				fareDAO.setCity((String)citySelect.getSelectedItem());
				fareDAO.setKms(Float.parseFloat(kms_input.getText().toString()));
				fareDAO.setNightFare(night_charge_check.isChecked());
				AutoFareService service=new AutoFareServiceImpl();
				final_fare.setText(numFormat.format(service.getFare(fareDAO)));
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about:
			startActivity(new Intent(this, AboutActivity.class));
			break;
		case R.id.exit:
			finish();
			break;
		}
		return true;
	}
}
